var type = "";
var name = "";
var page = 1;
var limit = 5;
var count = 0;

layui.use(['jquery', 'laypage'], function () {
    var $ = layui.jquery,
        laypage = layui.laypage;

    var active = {
        initColumn: function () {
            // 获取栏目列表
            $.ajax({
                url: "/interface/getColumnList",
                data: {},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.returnCode === "00") {
                        $(".ysh-list-left ul").empty();
                        var columnList = res.list;
                        if (!!columnList && columnList.length > 0) {
                            if (type == null || type == "") {
                                type = columnList[0].ID;
                                name = columnList[0].COLUMN_NAME;
                            }
                            for (var j = 0; j < columnList.length; j++) {
                                var act = "";
                                if (type != null && type != "" && columnList[j].ID == type) {
                                    act = "active";
                                    name = columnList[j].COLUMN_NAME;
                                } else if (type == null || type == "") {
                                    name = columnList[0].COLUMN_NAME;
                                }
                                var str = '<a href="list.html?type=' + columnList[j].ID + '">\n' +
                                    '          <li class="' + act + '">' + columnList[j].COLUMN_NAME + '</li>\n' +
                                    '      </a>';
                                $(".ysh-list-left ul").append(str);
                            }
                            $(".type-name").html(name);
                            active.initList();
                        }

                    } else if (res.returnCode === "01") {
                        console.log(res);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        },
        initList: function () {
            $.ajax({
                url: "/interface/getInformationList",
                data: {COLUMN_NAME: name, page: page, limit: limit},
                dataType: "json",
                type: "post",
                async: false,
                success: function (res) {
                    if (res.returnCode === "00") {
                        var list = res.data;
                        count = res.count;
                        $(".ysh-list-right-main").empty();
                        if (!!list && list.length > 0) {
                            for (var i = 0; i < list.length; i++) {
                                var cls = "";
                                if (list[i].IS_TOP == "1") {
                                    cls = 'ysh-list-right-item-first clearfix';
                                }
                                var host = location.host;
                                var src = "http://" + host + list[i].IMAGE_ADDRESS;
                                var str = '<div class="ysh-list-right-item ' + cls + '">\n' +
                                    '          <a href="detail.html?id=' + list[i].ID + '">\n' +
                                    '              <div class="ysh-list-right-item-l fl"><img src="' + src + '" alt="' + list[i].TITLE + '" style="max-height: 130px;">\n' +
                                    '              </div>\n' +
                                    '              <div class="ysh-list-right-item-r">\n' +
                                    '                  <p class="item-title">' + list[i].TITLE + '</p>\n' +
                                    '                  <p class="item-source-time"><span>来源 : ' + list[i].SOURCE + '</span><span>' + active.toDateString(new Date(list[i].GMT_CREATE)) + '</span></p>\n' +
                                    '                  <p class="item-page"><span>导语：</span>' + list[i].INTRODUCTION + '' +
                                    '                  </p>\n' +
                                    '              </div>\n' +
                                    '          </a>\n' +
                                    '      </div>';
                                $(".ysh-list-right-main").append(str);
                            }
                        }
                    } else if (res.returnCode === "01") {
                        console.log(res);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.error(XMLHttpRequest.status);
                    console.error(XMLHttpRequest.readyState);
                    console.error(textStatus);
                }
            });
        },
        //时间戳的处理
        toDateString: function (d, format) {
            var date = new Date(d || new Date())
                , ymd = [
                active.digit(date.getFullYear(), 4)
                , active.digit(date.getMonth() + 1)
                , active.digit(date.getDate())
            ]
                , hms = [
                active.digit(date.getHours())
                , active.digit(date.getMinutes())
                , active.digit(date.getSeconds())
            ];

            format = format || 'yyyy-MM-dd HH:mm:ss';

            return format.replace(/yyyy/g, ymd[0])
                .replace(/MM/g, ymd[1])
                .replace(/dd/g, ymd[2])
                .replace(/HH/g, hms[0])
                .replace(/mm/g, hms[1])
                .replace(/ss/g, hms[2]);
        },
        //数字前置补零
        digit: function (num, length, end) {
            var str = '';
            num = String(num);
            length = length || 2;
            for (var i = num.length; i < length; i++) {
                str += '0';
            }
            return num < Math.pow(10, length) ? str + (num | 0) : num;
        }
    };

    type = QueryUtils.GetQueryString("type");
    active.initColumn();
    //自定义首页、尾页、上一页、下一页文本
    laypage.render({
            elem: 'page'
            ,count: count
            ,limit: 5
            ,theme: '#ff6600'
            ,first: '首页'
            ,last: '尾页'
            ,prev: '上一页'
            ,next: '下一页'
            ,jump: function(obj){
                page = obj.curr;
                active.initList();
            }
        });

});

